/**  

* @Title: LoginAction.java 

* @Package com.gxbadge.action 

* @Description: TODO(用一句话描述该文件做什么) 

* @author CST

* @version V1.0  

*/ 

package com.gxbadge.action;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.gxbadge.action.base.BaseAction;
import com.gxbadge.model.Category;
import com.gxbadge.model.Language;
import com.gxbadge.model.Navigation;
import com.gxbadge.model.Operator;


public class NavigationAction extends BaseAction {
	
	private Navigation nav;
	private List<Navigation> navList;
	public Navigation getNav() {
		return nav;
	}
	public void setNav(Navigation nav) {
		this.nav = nav;
	}
	public List<Navigation> getNavList() {
		return navList;
	}
	public void setNavList(List<Navigation> navList) {
		this.navList = navList;
	}
	
	private String paramNavId;
	
	public String getParamNavId() {
		return paramNavId;
	}
	public void setParamNavId(String paramNavId) {
		this.paramNavId = paramNavId;
	}
	
	private Category cate;
	
	public Category getCate() {
		return cate;
	}
	public void setCate(Category cate) {
		this.cate = cate;
	}
	private Map<Navigation, List<Navigation>> navMap;
	private Map<Navigation, List<Navigation>> navMapLev2;
	public Map<Navigation, List<Navigation>> getNavMap() {
		return navMap;
	}
	public void setNavMap(Map<Navigation, List<Navigation>> navMap) {
		this.navMap = navMap;
	}
	
	public Map<Navigation, List<Navigation>> getNavMapLev2() {
		return navMapLev2;
	}
	public void setNavMapLev2(Map<Navigation, List<Navigation>> navMapLev2) {
		this.navMapLev2 = navMapLev2;
	}
	public String find_navList() throws Exception{
		if(this.getNavList()!=null){
			this.setNavList(null);
		}
		List<Navigation> navList = this.cycleNavList(0);
		if(navList!=null){
			this.setNavList(navList);
		}
		return "navList_find_success";
	}
	
	public List<Navigation> cycleNavList(int navPid){
		
		List<Navigation> cnList = new LinkedList<Navigation>();

		List<Navigation> navList = adminManager.findNavListByNavPid(navPid);
		
		if(navList!=null&&navList.size()>=1){

			for(int i=0;i<navList.size();i++){
				
				cnList.add(navList.get(i));
				List<Navigation> nList = this.cycleNavList(navList.get(i).getNavId());
				if(nList!=null&&nList.size()>=1){
					cnList.addAll(nList);
				}
			}
		}

		return cnList;
	}
	
	private String[] parentId;
	
	public String[] getParentId() {
		return parentId;
	}
	public void setParentId(String[] parentId) {
		this.parentId = parentId;
	}
	private Language lang;
	private List<Language> langList;
	
	public Language getLang() {
		return lang;
	}
	public void setLang(Language lang) {
		this.lang = lang;
	}
	public List<Language> getLangList() {
		return langList;
	}
	public void setLangList(List<Language> langList) {
		this.langList = langList;
	}
	
	private String selectLangId;	
	public String getSelectLangId() {
		return selectLangId;
	}
	public void setSelectLangId(String selectLangId) {
		this.selectLangId = selectLangId;
	}
	public String insert_nav() throws Exception {
		
		if(this.getNavList()!=null){
			this.setNavList(null);
		}
		if(this.getLangList()!=null){
			this.setLangList(null);
		}
		if(this.getNav()!=null&&this.getNav().getNavId()!=null&&this.getNav().getNavPid()==null){
			
			System.out.println("this.getNav().getNavId()----------"+this.getNav().getNavId());
			System.out.println("this.getNav().getNavPid()---------"+this.getNav().getNavPid());
			Navigation nav = adminManager.findNavByNavId(this.getNav().getNavId());
			if(nav!=null){	
				this.setNav(nav);
				List<Navigation> navList = this.cycleNavList(0);
				if(navList!=null&&navList.size()>=1){
					this.setNavList(navList);
					
				}
			}
		}else{
			
			List<Language> langList = adminManager.findLangAll();
			if(langList!=null&&langList.size()>=1){
				this.setLangList(langList);
			}
		}

		return "nav_insert_success";
	}
	
	public String add_nav() throws Exception {
		
		if(this.getParentId()!=null){
			
			int navId = Integer.parseInt(this.getParentId()[0]);
			Navigation nav = adminManager.findNavByNavId(navId);
			if(nav!=null){
				
				this.getNav().setNavPid(nav.getNavId());
				Language lang = nav.getLang();
				this.getNav().setLang(lang);
				byte levFlag = (byte) (nav.getLevFlag()+1);
				this.getNav().setLevFlag(levFlag);
			}
			adminManager.add_nav(this.getNav());
			this.setParentId(null);
			
		}else{
			
			this.getNav().setNavPid(0);
			if(this.getSelectLangId()!=null){
				int langId = Integer.parseInt(this.getSelectLangId());
				Language lang = adminManager.findLangByLangId(langId);
				this.getNav().setLang(lang);
				this.setSelectLangId(null);
			}
			byte levFlag = 0;
			this.getNav().setLevFlag(levFlag);
			adminManager.add_nav(this.getNav());
		}
		this.setNav(null);

		return "nav_add_success";
	}
	public String del_nav() throws Exception {
	
		if(this.getNav()!=null&&this.getNav().getNavId()!=null){
			
			Navigation nav = adminManager.findNavByNavId(this.getNav().getNavId());
			if(nav!=null){
				
				List<Navigation> navList = this.cycleNavList(nav.getNavId());
				if(navList!=null&&navList.size()>=1){
					
					for(int i=0;i<navList.size();i++){
						
						List<Operator> operList = adminManager.findOperListByNav(navList.get(i));
						if(operList!=null&&operList.size()>=1){
							for(int j=0;j<operList.size();j++){
								adminManager.del_oper(operList.get(j));
							}
						}
						adminManager.del_nav(navList.get(i));	
					}
				}
				adminManager.del_nav(nav);
			}
			
		}

		return "nav_del_success";
	}
	public String upload_nav() throws Exception{
		
		if(this.getNavList()!=null){
			this.setNavList(null);
		}
		if(this.getNav()!=null&&this.getNav().getNavId()!=null){
			
			Navigation nav = adminManager.findNavByNavId(this.getNav().getNavId());
			if(nav!=null){
				this.setNav(nav);
				List<Navigation> navList = this.cycleNavList(0);
				if(navList!=null&&navList.size()>=1){
					this.setNavList(navList);
				}
			}
			
		}

		return "nav_upload_success";
	}
	public String modify_nav() throws Exception{
		
		if(this.getParentId()!=null){
			
			int navPid = Integer.parseInt(this.getParentId()[0]);
			if(this.getNav().getNavId()!=navPid){
				this.getNav().setNavPid(navPid);
			}
			this.setParentId(null);
			
		}
		adminManager.modify_nav(this.getNav());
		this.setNav(null);
		return "nav_modify_success";
	}
	
	public String find_navOperList() throws Exception{
		
		if(this.getCate()!=null&&this.getCate().getCateId()!=null){
			
			Category cate = adminManager.findCateByCateId(this.getCate().getCateId());
			if(cate!=null){
				this.setCate(cate);
			}
			
		}
		if(this.getNavList()!=null){
			this.setNavList(null);
		}
		List<Navigation> navList = this.cycleNavList(0);
		if(navList!=null){
			this.setNavList(navList);
		}

		return "navOperList_find_success";
	}
	
	
	
	
	
}
